<?php

# php wrapper for wagg weather db. #
####################################

require_once("XMPPHP/XMPP.php");
require_once("config.php");

init();

function init()
{
    $waggDATA = new WaggDATA();

    $log = $waggDATA->GetLatestLog();
    date_default_timezone_set('Europe/Berlin');

    $report = "Weather@".date("H:i:s")."\n";
    $report .= "Temp: ".$log['temp_out']."° C"."\n";
    $report .= "Temp in: ".$log['temp_in']."° C"."\n";
    $report .= "Wind average: ".$log['wind_ave']." m/s"."\n";
    $report .= "Wind max: ".$log['wind_gust']." m/s"."\n";
    $report .= "Humidity: ".$log['hum_out']."\n";

    $waggDATA->ReportXMPP("recipient@host.com", $report);
}

class WaggDATA {

    # Get current (latest) temperature for station.
    public function GetTemperature($station) {
        return 1.00;
    }

    public function ReportXMPP($recipient, $message)
    {
        $conn = new XMPPHP_XMPP(XMPP_ADDR, 5222, XMPP_USER, XMPP_PASS, 'wagg', XMPP_HOST, $printlog=False, $loglevel=LOGGING_INFO);
        $conn->connect();
        $conn->processUntil('session_start');
        $conn->message($recipient, $message);
        $conn->disconnect();

    }

    public function GetLatestLog()
    {
        try
        {
            $sql = ("SELECT * FROM `wh1080` ORDER BY `id` DESC LIMIT 0,1");
            $dbh = new PDO(DB_CONNSTR,DB_USER,DB_PASS);

            $rows = $dbh->query($sql);
            $ret = Array();

            foreach($rows as $row)
            {
                $ret = $row;
            }
            $dbh = null;
            return $ret;
        }
        catch (PDOException $e)
        {
            print "Error: " . $e->getMessage() . "<br/>";
            die('Exited.');
        }
    }

}

?>
